using System;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;

namespace OracleInternal.TTC
{
	internal class TTCBFile : TTCLob
	{
		internal const int KOLBLXFXL = 16;

		internal const int KOLBLNAML = 2;

		internal const int KOLBLNMXL = 255;

		internal const int KOLBLLENL = 2;

		internal const int KOLBLVSNL = 2;

		internal const int KOLBLFLGL = 4;

		internal const int KOLBLBYTL = 2;

		internal const int KOLBLFIDB = 10;

		internal const int KOLBLXSPRB = 12;

		internal const int KOLBLDRLB = 16;

		internal const int KOLBLDIRB = 18;

		internal new const int KOLBLFLGB = 4;

		internal const int KOLBLBYTB = 8;

		internal TTCBFile(MarshallingEngine mEngine)
			: base(mEngine)
		{
		}

		internal override byte[] CreateTemporaryLob(bool bCache, bool bNClob, int duration)
		{
			throw new InvalidOperationException();
		}

		internal byte[] SetDirFileName(string directoryName, string fileName)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCBFile, OracleTraceFuncName.SetDirFileName);
			}
			try
			{
				int num = directoryName?.Length ?? 0;
				int num2 = fileName?.Length ?? 0;
				int num3 = 16 + num + num2 + 4;
				byte[] array = new byte[num3];
				array[16] = (byte)(num / 256);
				array[17] = (byte)(num % 256);
				byte[] array2 = null;
				byte[] array3 = null;
				int num4 = 0;
				if (directoryName != null)
				{
					array2 = m_marshallingEngine.m_dbCharSetConv.ConvertStringToBytes(directoryName, 0, directoryName.Length);
					num4 = array2.Length;
					for (int i = 0; i < num4; i++)
					{
						array[18 + i] = array2[i];
					}
				}
				int num5 = 18 + num4;
				array[num5] = (byte)(num2 / 256);
				array[num5 + 1] = (byte)(num2 % 256);
				if (fileName != null)
				{
					array3 = m_marshallingEngine.m_dbCharSetConv.ConvertStringToBytes(fileName, 0, fileName.Length);
					num4 = array3.Length;
					for (int j = 0; j < num4; j++)
					{
						array[num5 + 2 + j] = array3[j];
					}
				}
				array[0] = (byte)((num3 - 2) / 256);
				array[1] = (byte)((num3 - 2) % 256);
				array[4] = 8;
				array[8] = 0;
				array[9] = 1;
				array[2] = 0;
				array[3] = 1;
				array[10] = (array[11] = 0);
				array[5] = 8;
				return array;
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCBFile, OracleTraceFuncName.SetDirFileName, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCBFile, OracleTraceFuncName.SetDirFileName);
				}
			}
		}

		internal bool Open(byte[] lobLocator, int mode)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCBFile, OracleTraceFuncName.Open);
			}
			try
			{
				return OpenLob(lobLocator, 11, 256);
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCBFile, OracleTraceFuncName.Open, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCBFile, OracleTraceFuncName.Open);
				}
			}
		}

		internal bool Close(byte[] lobLocator)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCBFile, OracleTraceFuncName.Close);
			}
			try
			{
				return CloseLob(lobLocator, 512);
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCBFile, OracleTraceFuncName.Close, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCBFile, OracleTraceFuncName.Close);
				}
			}
		}

		internal bool Exists(byte[] lobLocator)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCBFile, OracleTraceFuncName.Exists);
			}
			try
			{
				Initialize();
				m_sourceLobLocator = lobLocator;
				m_lobOperation = 2048L;
				m_bNullO2U = true;
				WriteFunctionHeader();
				WriteLobOperation();
				m_marshallingEngine.m_oraBufWriter.FlushData();
				ReceiveResponse();
				return m_bLobNull;
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCBFile, OracleTraceFuncName.Exists, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCBFile, OracleTraceFuncName.Exists);
				}
			}
		}
	}
}
